﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneAppCheckSensors
{
    using Microsoft.Devices.Sensors;
    using Microsoft.Xna.Framework;

    public partial class PageMotion : PhoneApplicationPage
    {
        public PageMotion()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (mySensor == null)
            {
                mySensor = new Motion();
                mySensor.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(mySensor_CurrentValueChanged);
            }

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (buttonStop.IsEnabled)
            {
                mySensor.Stop();
                buttonStart.IsEnabled = true;
                buttonStop.IsEnabled = false;
                buttonDraw.IsEnabled = false;
            }
            base.OnNavigatedFrom(e);
        }

        private Motion mySensor;
        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            gpAP.AdjustDrawArea();
            gpAR.AdjustDrawArea();
            gpAY.AdjustDrawArea();
            gpAclX.AdjustDrawArea();
            gpAclY.AdjustDrawArea();
            gpAclZ.AdjustDrawArea();
            gpRRX.AdjustDrawArea();
            gpRRY.AdjustDrawArea();
            gpRRZ.AdjustDrawArea();
            gpGrvX.AdjustDrawArea();
            gpGrvY.AdjustDrawArea();
            gpGrvZ.AdjustDrawArea();

            buttonStart.IsEnabled = false;
            buttonStop.IsEnabled = true;
            buttonDraw.IsEnabled = true;
            mySensor.Start();
        }

        void mySensor_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
        {
            Dispatcher.BeginInvoke(() => {
                tbTimestamp.Text = e.SensorReading.Timestamp.ToString();
                float attitudePitch = e.SensorReading.Attitude.Pitch;
                float attitudeRoll = e.SensorReading.Attitude.Roll;
                float attitudeYaw = e.SensorReading.Attitude.Yaw;

                float accelX = e.SensorReading.DeviceAcceleration.X;
                float accelY = e.SensorReading.DeviceAcceleration.Y;
                float accelZ = e.SensorReading.DeviceAcceleration.Z;
                float rrX = e.SensorReading.DeviceRotationRate.X;
                float rrY = e.SensorReading.DeviceRotationRate.Y;
                float rrZ = e.SensorReading.DeviceRotationRate.Z;

                float gravityX = e.SensorReading.Gravity.X;
                float gravityY = e.SensorReading.Gravity.Y;
                float gravityZ = e.SensorReading.Gravity.Z;

                tbAttdP.Text = String.Format("{0:0.0000}", attitudePitch);
                tbAttdR.Text = String.Format("{0:0.0000}", attitudeRoll);
                tbAttdY.Text = String.Format("{0:0.0000}", attitudeYaw);
                tbAccl.Text = String.Format("X={0:0.0},Y={1:0.0},Z={2:0.0}", accelX, accelY, accelZ);
                tbRR.Text = String.Format("X={0:0.0},Y={1:0.0},Z={2:0.0}", rrX, rrY, rrZ);

                tbGravity.Text = String.Format("X={0:0.0000},Y={1:0.0000},Z={2:0.0000}", gravityX, gravityY, gravityZ);

                gpAP.AddPoint(e.SensorReading.Timestamp, attitudePitch);
                gpAR.AddPoint(e.SensorReading.Timestamp, attitudeRoll);
                gpAY.AddPoint(e.SensorReading.Timestamp, attitudeYaw);

                gpAclX.AddPoint(e.SensorReading.Timestamp, accelX);
                gpAclY.AddPoint(e.SensorReading.Timestamp, accelY);
                gpAclZ.AddPoint(e.SensorReading.Timestamp, accelZ);

                gpRRX.AddPoint(e.SensorReading.Timestamp, rrX);
                gpRRY.AddPoint(e.SensorReading.Timestamp, rrY);
                gpRRZ.AddPoint(e.SensorReading.Timestamp, rrZ);

                gpGrvX.AddPoint(e.SensorReading.Timestamp, gravityX);
                gpGrvY.AddPoint(e.SensorReading.Timestamp, gravityY);
                gpGrvZ.AddPoint(e.SensorReading.Timestamp, gravityZ);
            });
        }

        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {
            buttonStop.IsEnabled = false;
            buttonStart.IsEnabled = true;
            mySensor.Stop();
        }

        private void buttonDraw_Click(object sender, RoutedEventArgs e)
        {
            gpAP.DrawGraph();
            gpAR.DrawGraph();
            gpAY.DrawGraph();

            gpAclX.DrawGraph();
            gpAclY.DrawGraph();
            gpAclZ.DrawGraph();

            gpRRX.DrawGraph();
            gpRRY.DrawGraph();
            gpRRZ.DrawGraph();

            gpGrvX.DrawGraph();
            gpGrvY.DrawGraph();
            gpGrvZ.DrawGraph();
        }
    }
}